<?php
	####################################################
	# library import and object instance.              #
	####################################################
	include_once('./common.php');


	####################################################
	# request data($_POST AND $_GET FROM $_REQUEST)    #
	####################################################
	# REQUEST(get and post variables)
	$req = Request::clean_request($_REQUEST, FALSE);

	# GET(url variables)

	# POST(form variables)

	# Validation(special case validate)
	//*/
	Validation::check_is_emptys($req);
	Validation::check_is_idpw($req['mid']);
	Validation::check_is_email($req['memail']);
	//Nlog::xmp($req); //display clean and remmap values
	//*/
	####################################################
	# implementation                                   #
	####################################################

	//*/
	# 회원정보를 조회 한다.
	$ret = $db->get(
		sprintf("SELECT * FROM `n_member` WHERE `mid`='%s' AND `memail`='%s'",
			$req['mid'],
			$req['memail']
		)
	);
	//Nlog::xmp($ret);

	if($ret['has']){
		# 세션에 아이디를 저장한다.
		$_SESSION['muid'] = $ret['res']['muid'];

		# 인증키를 만든 후 이메일로 전송 한다.
		$temp_passwd = CharacterGeneration::random_string(10);

		$mail = new Mailer();
		$retmail = $mail->send($req['memail'], "[".TITLE."] 이메일 인증", sprintf('<table border="1"><tr><td> 이메일 인증 : %s</td></tr></table>', $temp_passwd));
		if($retmail){
			//Nlog::xmp("발송 성공");
			$retcert = $db->query(
				sprintf("INSERT INTO `n_certmail` VALUES(NULL, '%s', '%s')",
					$req['memail'],
					$temp_passwd
				)
			);
			if($retcert){
				//Nlog::xmp("n_certmail 인서트 성공");
				$flag_cert = TRUE;
			} else {
				//Nlog::xmp("n_certmail 인서트 실패");
				$flag_cert = FALSE;
			}
		} else {
			//Nlog::xmp("발송실패");
			$flag_cert = FALSE;
		}
	}
	//*/
	//Nlog::xmp($ret);

?>
<?php
	$html->custom_header(TITLE);
	$html->custom_js("{$JS}jquery.1.7.1.min.js");
	$html->custom_js("{$JS}lib.control.js");
	$html->custom_css("{$CSS}basic.css");
?>
<script type="text/javascript" language="javascript">
//<![CDATA[
	var PageAction = {
		validate:function(){
			//*/
			var check = [
				Validation.check_is_null("#cert_email", "인증코드를 입력하세요.")
			];
			return Validation.check(check);
			//*/
		}
	}
//]]>
</script>
<?$html->custom_body();?>
<?=$tmp->tiles_header()?>
	<h1>비밀번호 찾기</h1>
	<?php if($flag_cert): ?>
		귀하의 이메일 주소 <?=$req['memail']?>로 인증코드를 발송 하였습니다.
		<form id="formx" name="formx" method="post" action="./sign_passwd_find_change_passwd.php" onsubmit="return PageAction.validate();">
			<table width="100%" border="1" cellspacing="3" cellpadding="0">
				<input type="hidden" name="cert_email" id="cert_email" value="<?=$req['memail']?>" />
				<tr>
					<th>
						인증코드 입력
					</th>
					<td>
						<input type="text" name="cert_key" id="cert_key" maxlength="10" />
					</td>
				</tr>
				<tr>
					<th colspan="2">
						<?=NTool::submit_btn();?>
					</th>
				</tr>
			</table>
		</form>
	<?php else: ?>
		일치하는 정보가 없습니다.
	<?php endif; ?>
	<?=NTool::btn("뒤로", "Mover.back();");?>
	<?=NTool::btn("인덱스", "Mover.home();");?>
<?=$tmp->tiles_footer()?>
<?php $html->html_footer(); ?>